<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<link rel="stylesheet" href="./layui/css/layui.css"  media="all">
</head>
<body>

<form class="layui-form">
  <div class="layui-form-item">
    <label class="layui-form-label">商品名称</label>
    <div class="layui-input-block">
      <input type="text" name="goods_name" lay-verify="goods_name" autocomplete="off" placeholder="请输入标题" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">商品类型</label>
    <div class="layui-input-block">
      <input type="text" name="goods_type" lay-verify="goods_type" lay-reqtext="用户名是必填项，岂能为空？" placeholder="请输入" autocomplete="off" class="layui-input">
    </div>
  </div>
    <div class="layui-inline">
      <label class="layui-form-label">商品品牌</label>
      <div class="layui-input-inline">
        <input type="text" name="goods_brand" lay-verify="goods_brand" autocomplete="off" class="layui-input">
      </div>
    </div>
  </div>
    <div class="layui-inline">
      <label class="layui-form-label">商品价格</label>
      <div class="layui-input-inline">
        <input type="text" name="goods_price" lay-verify="goods_price" autocomplete="off" class="layui-input">
      </div>
    </div>
  </div>

    <div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
</form>
 	


</body>
</html>
<script src="./layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
layui.use(['form', 'layedit', 'laydate'], function(){
  var form = layui.form
  ,layer = layui.layer
  ,layedit = layui.layedit
  ,laydate = layui.laydate;
  var $=layui.$;
  
 
 
  //自定义验证规则
  form.verify({
    goods_name: function(value){
      if(value.length < 5){
        return '标题至少得5个字符啊';
      }
    }
    ,goods_type: [
      /^[\S]{6,12}$/
      ,'类型必须6到12位，且不能出现空格'
    ]
    ,content: function(value){
      layedit.sync(editIndex);
    }
  });
  
 
  //监听提交
  form.on('submit(demo1)', function(data){
    // layer.alert(JSON.stringify(data.field), {
    //   title: '最终的提交信息'
    // })
         $.ajax({
         	  method:'GET',
         	  url:'http://127.0.0.1/localhost/zhuchao/tp/public/index.php/index/index/add',
         	  data:data.field,
         	  dataType:'JSON',
         	  success:function(res){
         	  	 console.log(res)
         	  	 if(res.code==0){
         	  	 	 layer.alert(res.msg,function(){
         	  	 	 	  location.href="./show.html"
         	  	 	 })
         	  	 }
         	  }
         })
    return false;
  });
 
  
  
});
</script>